<!DOCTYPE html>
<html>

	<head lang="en">
		<title>找攻略</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/strategy.css" />
		<script src="/js/plugins/jrender/jrender.min.js"></script>
		<link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
		<script src="/js/plugins/dialog2/dialog.min.js"></script>
		<script>
            $(function () {
                var placeId;
                //获取每月推荐游记数据
                $.get("/travels/commends",{type:1,pageSize:1},function (data) {
                    //将数据进行渲染
                    $("#travelsCommends").renderValues({list:data.list[0]},{
                        getUrl:function (item,value) {//参数1:获取贴有 render-fun="getUrl"属性的元素;参数2:可获取的参数
                            var url = $(item).data("href");
                            $(item).attr("href",url+value);//将id拼接在资源名后
                        }
                    });
                })
				//获取所有状态为推荐的地区
				$.get("/regions",{state:1},function (data) {
				    //拼接标签栏标签
                    var temp = "";
                    $.each(data,function (index,ele) {
						temp += '<li class="nav-item"><a data-rid="' + ele.id + '" class="nav-link" data-toggle="pill" href="#pills-' + ele.id + '">' + ele.name + '</a></li>';
                    })
                    $("#pills-tab").append(temp);

                    //给所有的标签栏绑定点击事件
                    $(".nav-link[data-rid]").click(function () {
                        placeId = $(this).data("rid");
                        //重置分页参数,防止叠加
                        currentPage = 2;
                        pages = 2;
                        //获取某个地区下状态为推荐的攻略(获取html)
						$.get("/regions/"+placeId+"/strategies",function (data) {
							$("#pills-1").html(data);
                        },"html");
                    });
                })
				//获取大攻略中状态时推荐的数据(不分页)
				$.get("/strategies",{state:1,pageSize:0},function (data) {
                    console.log(data);
                    //对当季推荐进行数据渲染
					$(".strategyCommend").renderValues(data,{
                        getUrl:function (item,value) {//参数1:获取贴有 render-fun="getUrl"属性的元素;参数2:可获取的参数
                            var url = $(item).data("href");
                            $(item).attr("href",url+value);//将id拼接在资源名后
                        }
					})
                });
				//=======================分页=============================
                var currentPage = 2;//默认首页为2,第一页由模板提供
                var pages = 2;//存储总页数的容器,首次没有值,需要先赋值
                var loading = false;//标志,用来解决尾页多次触发查询的问题
                //抽取分页查询
                function query() {
                    //如果正在查询,就结束
                    if (loading) {
                        return;
                    }
                    //不在查询就查询,并修改标志为正在查询
                    loading = true;
                    //获取所有已发布游记数据
                    $.get("/regions/"+placeId+"/strategies",{currentPage:currentPage},function (data) {
                        console.log(data);
                        //利用模板,将分页的数据拼接到后面
						var div = $("#template .mb").clone(true);
						$.each(data.list,function (index,ele) {
                            console.log(ele);
                            $(div).find("a").attr("href","../strategyCatalogs.html?id"+ele.id);
                            $(div).find("img").attr("src",ele.coverUrl);
							$(div).find("span").html(ele.title);
                            $(".classify").append(div);
                        })
                        pages = data.pages;//获取总分页数
                        currentPage++;
                        //执行完查询之后,修改标志为不查询
                        loading = false;
                    },"json")
                }
                //给屏幕绑定滑动事件
                $(window).scroll(function () {
                    //屏幕滚动触发分页查询(当屏幕的总高度加上溢出的总长度大于等于查询过来页面的长度时)
                    if ($(window).height() + $(document).scrollTop() >= $(document).height()) {
                        if (currentPage <= pages) {
                            if (placeId) {//防止推荐页也查询
                                query();//第二次查询
                            }
                        }else {
                            //提醒,没有更多数据了
                            $(document).dialog({
                                type: 'notice',
                                infoText: '没有更多数据了',
                                autoClose: 1500,
                                position: 'bottom'  // center: 居中; bottom: 底部
                            });
                        }
                    }
                })
            })
		</script>
	</head>

	<body>
	<div class="search-head">
		<div class="row nav-search">
			<div class="col-2">
				<a href="index.html">
					<span><i class="fa fa-chevron-left fa-2x"></i></span>
				</a>
			</div>
			<div class="col-10">
				<div class="input-group-sm search">
					<input class="form-control searchBtn" placeholder="找攻略">
				</div>
			</div>
		</div>
	</div>
	<!--分页模板-->
	<div id="template">
		<div class="col-6 mb">
			<a href="">
				<img class="float-left " src="">
				<div class="classify-text">
					<span></span>
					<p>0人收藏</p>
				</div>
			</a>
		</div>
	</div>

	<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab" role="tablist">
		<li class="nav-item">
				<a class="nav-link active show"  data-toggle="pill" href="#pills-top">
				<span style="font-weight: bold;">推荐</span>
			</a>
		</li>
	</ul>

	<div class="tab-content" id="pills-tabContent">
		<div class="tab-pane fade active show" id="pills-top">
			<div class="container commend" id="travelsCommends">
				<a data-href="travelContent.html?id=" render-fun="getUrl" render-key="list.travelId">
					<img  render-src="list.coverUrl">
					<p render-html="list.title"></p>
				</a>
			</div>
			<hr>
			<div class="container strategyCommend">
				<h6>当季推荐</h6>
				<div class="row hot" render-loop="list">
					<div class="col-4">
						<a data-href="strategyCatalogs.html?id=" render-fun="getUrl" render-key="list.id">
							<img  render-src="list.coverUrl">
							<p render-html="list.title"></p>
						</a>
					</div>
				</div>
			</div>
		</div>
		<div class="tab-pane fade" id="pills-1">

		</div>
	</div>
	</body>

</html>